package com.cardrive.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Utils {

	/**
	 * Static method to allow decode a bitmap using resources and a resource ID
	 * 
	 * @param resource
	 *            get the resources from the application
	 * @param resourceID
	 *            get the current element to load in bitmap data.
	 * @return a bitmap when load is complete.
	 */
	public static Bitmap decodeBitmap(Resources resource, int resourceID) {
		Bitmap bmp = null;
		Options options = new Options();
		options.inPurgeable = true;
		options.inDither = false;
		options.inJustDecodeBounds = false;
		options.inSampleSize = 1;
		options.mCancel = false;
		// options.inPreferredConfig = Bitmap.Config.RGB_565;
		bmp = BitmapFactory.decodeResource(resource, resourceID, options);
		return bmp;
	}

	/**
	 * Verify if exists a network available in the device.
	 * 
	 * @param context
	 *            is the value from the current class is calling this method.
	 * @return true if exists an available network.
	 */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInformation = connectivityManager
				.getActiveNetworkInfo();
		return networkInformation != null;
	}

	/**
	 * Resize a bitmap in reference a max size.
	 * 
	 * @param bitmap
	 *            is the graphic to resize.
	 * @param maxSide
	 *            is a variable to resize proportionally the bitmap
	 * @return a resized bitmap.
	 */
	public static Bitmap resizeBitmap(Bitmap bitmap, int maxSide) {
		int currentHeight = bitmap.getHeight();
		int currentWidth = bitmap.getWidth();

		int scaledWidth = (currentWidth >= currentHeight) ? maxSide
				: (int) ((float) maxSide * ((float) currentWidth / (float) currentHeight));
		int scaledHeight = (currentHeight >= currentWidth) ? maxSide
				: (int) ((float) maxSide * ((float) currentHeight / (float) currentWidth));

		Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth,
				scaledHeight, true);
		return scaledBitmap;
	}

	/**
	 * Method exclusive for Dinosaurs whoe function is get the data from one
	 * file.
	 * 
	 * @param context
	 *            is the data from the current class.
	 * @param path
	 *            is the file to load.
	 * @return a matrix of Integers
	 */
	public static ArrayList<ArrayList<Integer>> getMapFromAssets(
			Context context, String path) {
		int cont = 0, lineLength = 0;
		ArrayList<ArrayList<Integer>> lines = new ArrayList<ArrayList<Integer>>();

		AssetManager am = context.getAssets();
		InputStreamReader is;
		try {
			is = new InputStreamReader(am.open(path));

			BufferedReader br = new BufferedReader(is);
			String line;
			while ((line = br.readLine()) != null) {
				ArrayList<Integer> token = new ArrayList<Integer>();
				lineLength = line.length();
				while (cont < lineLength) {
					token.add(Integer.parseInt(line.substring(cont, cont + 1)));
					cont++;
				}
				cont = 0;
				lines.add(token);
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return lines;
	}
}